package com.lz.springboot.apollo.rabbit;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

	public static final String SPRINGBOOT_RABBIT_SIMPLE_QUEUE = "springboot.rabbit.simple.queue";
	public static final String SPRINGBOOT_RABBIT_USER_QUEUE = "springboot.rabbit.user.queue";
	
	public static final String SPRINGBOOT_RABBIT_TOPIC_MESSAGE_QUEUE = "springboot.rabbit.topic.message.queue";
	public static final String SPRINGBOOT_RABBIT_TOPIC_MESSAGES_QUEUE = "springboot.rabbit.topic.messages.queue";
	
	public static final String SPRINGBOOT_RABBIT_FANOUT_A_QUEUE = "springboot.rabbit.fanout.a.queue";
	public static final String SPRINGBOOT_RABBIT_FANOUT_B_QUEUE = "springboot.rabbit.fanout.b.queue";
	public static final String SPRINGBOOT_RABBIT_FANOUT_C_QUEUE = "springboot.rabbit.fanout.c.queue";
	
	public static final String SPRINGBOOT_RABBIT_DIRECT_A_QUEUE = "springboot.rabbit.direct.a.queue";
	public static final String SPRINGBOOT_RABBIT_DIRECT_B_QUEUE = "springboot.rabbit.direct.b.queue";
	
	public static final String SPRINGBOOT_RABBIT_TOPIC_EXCHANGE = "springboot.rabbit.topic.exchage";
	public static final String SPRINGBOOT_RABBIT_FANOUT_EXCHANGE = "springboot.rabbit.fanout.exchage";
	public static final String SPRINGBOOT_RABBIT_DIRECT_EXCHANGE = "springboot.rabbit.direct.exchage";
	
	/**
	 * 创建队列
	 * @return
	 */
	@Bean
	public Queue simpleQueue(){
		return new Queue(SPRINGBOOT_RABBIT_SIMPLE_QUEUE);
	}
	
	@Bean
	public Queue userQueue(){
		return new Queue(SPRINGBOOT_RABBIT_USER_QUEUE);
	}
	
	
    @Bean
    public Queue topicMessageQueue() {
        return new Queue(SPRINGBOOT_RABBIT_TOPIC_MESSAGE_QUEUE);
    }

    @Bean
    public Queue topicMessagesQueue() {
        return new Queue(SPRINGBOOT_RABBIT_TOPIC_MESSAGES_QUEUE);
    }
    
    
    @Bean
    public Queue fanoutQueueA() {
        return new Queue(SPRINGBOOT_RABBIT_FANOUT_A_QUEUE);
    }

    @Bean
    public Queue fanoutQueueB() {
    	return new Queue(SPRINGBOOT_RABBIT_FANOUT_B_QUEUE);
    }

    @Bean
    public Queue fanoutQueueC() {
    	return new Queue(SPRINGBOOT_RABBIT_FANOUT_C_QUEUE);
    }
    
    @Bean
    public Queue directQueueA() {
    	return new Queue(SPRINGBOOT_RABBIT_DIRECT_A_QUEUE);
    }
    
    @Bean
    public Queue directQueueB() {
    	return new Queue(SPRINGBOOT_RABBIT_DIRECT_B_QUEUE);
    }
    
    /**
     * 创建Exchange
     * @return
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(SPRINGBOOT_RABBIT_TOPIC_EXCHANGE);
    }
    
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(SPRINGBOOT_RABBIT_FANOUT_EXCHANGE);
    }
    
    @Bean
    public DirectExchange directExchange() {
    	return new DirectExchange(SPRINGBOOT_RABBIT_DIRECT_EXCHANGE);
    }
    
    /**
     * 将队列'springboot.rabbit.topic.message.queue'与'springboot.rabbit.topic.exchage'绑定，binding_key为topic.message,就是完全匹配
     * @param topicMessageQueue
     * @param exchange
     * @return
     */
    @Bean
    public Binding bindingExchangeMessage(Queue topicMessageQueue, TopicExchange exchange) {
        return BindingBuilder.bind(topicMessageQueue).to(exchange).with("topic.message");
    }
    
    /**
     * 将队列'springboot.rabbit.topic.messages.queue'与'springboot.rabbit.topic.exchage'绑定，binding_key为topic.message,就是完全匹配
     * @param topicMessagesQueue
     * @param exchange
     * @return
     */
    @Bean
    public Binding bindingExchangeMessages(Queue topicMessagesQueue, TopicExchange exchange) {
    	return BindingBuilder.bind(topicMessagesQueue).to(exchange).with("topic.#");
    }
    
    
    @Bean
    public Binding bindingExchangeA(Queue fanoutQueueA,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueueA).to(fanoutExchange);
    }

    @Bean
    public Binding bindingExchangeB(Queue fanoutQueueB, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueueB).to(fanoutExchange);
    }

    @Bean
    public Binding bindingExchangeC(Queue fanoutQueueC, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueueC).to(fanoutExchange);
    }
    
    @Bean
    public Binding bindingDirectExchangeA(Queue directQueueA,DirectExchange directExchange) {
        return BindingBuilder.bind(directQueueA).to(directExchange).with("a");
    }
    
    @Bean
    public Binding bindingDirectExchangeB(Queue directQueueB,DirectExchange directExchange) {
    	return BindingBuilder.bind(directQueueB).to(directExchange).with("b");
    }

}
